﻿using System;

public class Plane:Primitive
{
    public Vector3 point;
    public Vector3 N;
    public Plane(Vector3 point, Vector3 N)
    {
        this.point = point;
        this.N = N;
        this.material = new Material();
    }
    public Plane(Vector3 point, Vector3 N, LightIntensity ambient, LightIntensity diffuse, LightIntensity specular)
	{
        this.point = point;
        this.N = N;
        this.material = new Material(ambient, diffuse, specular);
	}
    public override string ToString()
    {
        return "plane{p" + point + ", N=" + N + "}";
    }
    public override Vector3 getNormal(Vector3 point)
    {
        N.normalize();
        return N;
    }
    public override Vector3 getCentre()
    {
        return point;
    }
    public override float getWidth()
    {
        return 0;
    }
    public override bool intersect(Ray r)
    {
        float dot1 = this.N.dot(r.direction);
        if (dot1 == 0)
        {
            //Console.WriteLine("Promien biegnie rownolegle do plaszczyzny");
            return false;
        }
        else
        {
            float dot2 = this.N.dot(this.point);
            float dot3 = this.N.dot(r.origin);
            float t = (dot2 - dot3) / dot1;
            if (t > Plane.plusZero)
            {
                if (r.distance > t || r.distance == 0)
                {
                    r.distance = t;
                    r.destination = new Vector3(r.origin + r.distance * r.direction);
                    //Console.WriteLine("Promien przecina plaszczyzne w punkcie i=" + r.destination);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                //Console.WriteLine("Promien startuje za plaszczyzna");
                return false;
            }
        }
    }
}
